namespace Drill.Core.Utilities
{
	/// <summary>
	/// Converts string representations of boolean values to a <see cref="bool"/>.
	/// </summary>
    public static class StringToBooleanConverter
    {
		/// <summary>
		/// Converts the specified string value.
		/// </summary>
		/// <param name="value">The string representation of a boolean value.</param>
		/// <param name="defaultValue">
		/// The default value to be returned if the <paramref name="value"/> cannot be parsed.
		/// </param>
		/// <returns>
		/// An <see cref="bool"/> value that was either converted from the specified 
		/// <paramref name="value"/> of the specified <paramref name="defaultValue"/>.
		/// </returns>
        public static bool Convert(string value, bool defaultValue)
        {
            // If the value is null or empty, 
            // we cannot parse the boolean value.
            // In this case, return the defaultValue.
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }

            string valueToMatch = value.ToLowerInvariant().Trim();

            switch (valueToMatch)
            {
                case "false":
                case "f":
                case "no":
                case "n":
                case "0":
                    return false;

                case "true":
                case "t":
                case "yes":
                case "y":
                case "1":
                    return true;
            }

            // If we haven't figured out the boolean value at this point, 
            // attempt to parse the value using the Systen.Boolean class.
            // If th parsing fails, return the defaultValue.

            bool result;

            return bool.TryParse(value, out result) ? result : defaultValue;
        }
    }
}